<?php
// contact to database
$connect = mysql_connect("localhost", "root", "") or die ("Error , check your server connection.");
mysql_select_db("stms");
 
//Get data in local variable
$Regno=$_POST['Regno'];
$Title=$_POST['Title'];
$Firstname=$_POST['Firstname'];
$Lastname=$_POST['Lastname'];
$NICno=$_POST['NICno'];
$radio=$_POST['radio'];
$Civilstatus=$_POST['Civilstatus'];
$DOB=$_POST['DOB'];
$Nationality=$_POST['Nationality'];
$Religion=$_POST['Religion'];
$PermanentAddress=$_POST['PermanentAddress'];
$TemporaryAddress=$_POST['TemporaryAddress'];
$Faculty=$_POST['Faculty'];
$CourseOfStudy=$_POST['CourseOfStudy'];
$Academicyear=$_POST['Academicyear'];
$District=$_POST['District'];/*
echo $Regno."<br>";
echo $Title."<br>";
echo $Firstname."<br>";
echo $Lastname."<br>";
echo $NICno."<br>";
echo $radio."<br>";
echo $Civilstatus."<br>";
echo $DOB."<br>";
echo $Nationality."<br>";
echo $Religion."<br>";
echo $PermanentAddress."<br>";
echo $TemporaryAddress."<br>";
echo $Faculty."<br>";
echo $CourseOfStudy."<br>";
echo $Academicyear."<br>";
echo $District."<br>";


*/
$result=mysql_query("SELECT * FROM basicdetails WHERE Regno='$Regno'");
 $insert="Add";
 $update="Update";
 $delete="Delete";
 
 if($_POST['ADD']==$insert)
 {
 
 if (mysql_num_rows($result) == 1){
 
 	 header("Location:http://localhost/stms/pages/showadmin.php?err=This Regno is already exist!");

 //echo "This Regno is already exist";
 }
 else
 {
// check for null values
	//if ($Regno=="" or $Title=="" or $Firstname=="" or $Lastname=="" or $NICno=="" or $radio=="" or $Civilstatus=="" or $DOB=="" or $Nationality=="" or $Religion=="" or $PermanentAddress=="" or $TemporaryAddress=="" or $Faculty=="" or $CourseOfStudy=="" or $Academicyear=="" or $District=="")
	 //header("Location:http://localhost/stms/pages/showadmin.php?err=All fields must be entered, hit back button and re-enter information!");
	if($Regno=="")
	{
		 header("Location:http://localhost/stms/pages/showadmin.php?err=Regno must be entered, hit back button and re-enter information!");

	}
	else{
	$query="insert into basicdetails values('$Regno','$Title','$Firstname','$Lastname','$NICno','$radio','$Civilstatus','$DOB','$Nationality','$Religion','$PermanentAddress','$TemporaryAddress','$Faculty','$CourseOfStudy','$Academicyear','$District')";
	mysql_query($query)  or die(mysql_error());
		 header("Location:http://localhost/stms/pages/showadmin.php?err=Your message has been received!");

	//echo "Your message has been received";
	//}
	}
}
}
else if($_POST['UPDATE']==$update)
{
if (mysql_num_rows($result) == 1)
{
	$upd="UPDATE basicdetails SET Title='$Title',Firstname='$Firstname',Lastname='$Lastname',NICno='$NICno',Gender='$radio',Civilstatus='$Civilstatus',DOB='$DOB',Nationality='$Nationality',Religion='$Religion',Permanentaddress='$PermanentAddress',Temporaryaddress='$TemporaryAddress',Faculty='$Faculty',CourseOfStudy='$CourseOfStudy',AcademicYear='$Academicyear',District='$District' WHERE Regno='$Regno'";

	mysql_query($upd)  or die(mysql_error());
	header("Location:http://localhost/stms/pages/showadmin.php?err=Updated successfully!");

	//echo "Updated successfully";
 }
 
 else header("Location:http://localhost/stms/pages/showadmin.php?err=There is no Regno as you given!");
 //echo "There is no Regno as you given";
 }
 else if($_POST['DELETE']==$delete)
 {
 
if (mysql_num_rows($result) == 1)
 {
 $del="DELETE FROM basicdetails WHERE Regno = '$Regno'";
 mysql_query($del)  or die(mysql_error());
 header("Location:http://localhost/stms/pages/showadmin.php?err=Deleted successfully!");
	//echo "Deleted successfully";
	}
	else
	header("Location:http://localhost/stms/pages/showadmin.php?err=Regno is not there!");
	//echo "Regno is not there";
	
 
 }
 if(isset($_POST['Search']))
 {
	header("Location:http://localhost/stms/pages/showadmin.php?err1=$Regno");
 }
 if(isset($_POST['Search2']))
 {
  $se=$_POST['Searcher'];
	header("Location:http://localhost/stms/pages/showadmin.php?err2=$se");
 }
?>